"use strict";
const utils = require('../utils/util');
const methodEnums = {
	on: 'on',
	off: 'off',
};

module.exports = {
	meta: {
		docs: {
			description: "remind off listener after on listener",
			category: "Best Practices",
			recommended: false
		},
		fixable: null,
		schema: [
		]
	},
	create: function(context) {
		let onListeners = [], offListeners = [];
		return {
			CallExpression(node) {
				const member = utils.getCallExpression(node);
				if (member && member.methodName === methodEnums.on) {
					onListeners.push({
						targetName: member.targetObj,
						methodName: member.methodName,
						node,
					});
				}
				if(member &&  member.methodName === methodEnums.off) {
					offListeners.push({
						targetName: member.targetObj,
						methodName: member.methodName,
						node,
					});
				}
			},
			'Program:exit'() {
				if (onListeners.length > 0) {
					onListeners.forEach(obj => {
						const offListener = offListeners.find(item => item.targetName === obj.targetName && item.eventType === obj.eventType);
						if (!offListener) { // 没找到remove
							context.report(obj.node, `请别忘记移除${obj.targetName}的监听`);
						}
					});
				}
			},
		}
	},
};